<?php
defined('OA_LIB_PATH') || define('OA_LIB_PATH', '/opt/op/OALib');
defined('OPEN_LIB_PATH') || define('OPEN_LIB_PATH', '/opt/op/OpenLib');

require_once 'PHPUnit/Framework.php';
require_once 'Log.php';
require_once OPEN_LIB_PATH . '/open/models/OaAppModel.php';

class OaAppModelTest extends PHPUnit_Framework_TestCase
{
    private $_logger;
    private $_model;
    
    protected function setUp()
    {
        $logger = Log::factory('console', '', '', array(), PEAR_LOG_INFO);
        $this->_logger = $logger;
        $this->_model = new OaAppModel(OaAppModel::APP_CONFIG_ENV_TEST, $logger);
    }
    
    protected function tearDown()
    {
        
    }
    
    public function testCreateApp()
    {
        $this->markTestSkipped();
        $result = $this->_model->createApp('开心农场', 'jaggerwang@gmail.com', 1, array(
            'type' => OaAppModel::APP_TYPE_CLIENT,
            'pay_type' => OaAppModel::APP_PAY_TYPE_OA_TRANSFER,
            'service_login' => array(
                'ip' => '127.234.34.18',
                'alias' => 'alias'
            ),
            'app_pub_key' => 'app pub key'
        ));
        $this->assertTrue($result > 0);
        return $result;
    }
    
    /**
     * @depends testCreateApp
     */
    public function testGetApp($appId)
    {
        $this->markTestSkipped();
        $result = $this->_model->getApp($appId);
        $this->_logger->log($result, PEAR_LOG_INFO);
        $this->assertTrue(is_array($result));
    }
    
    /**
     * @depends testCreateApp
     */
    public function testModifyApp($appId)
    {
        $this->markTestSkipped();
        $result = $this->_model->modifyApp($appId, array(
        	'type' => OaAppModel::APP_TYPE_WEB_PAGE,
            'service_ibw' => array(
                'config1' => 'config2 value',
                'config2' => 'config2 value'
            )
        ));
        $this->assertTrue($result);
    }
    
    /**
     * @dataProvider appProvider
     */
    public function testGetAppDevelopers($appId)
    {
        $this->markTestSkipped();
        $result = $this->_model->getAppDevelopers($appId);
        $this->_logger->log($result, PEAR_LOG_INFO);
        $this->assertTrue(is_array($result));
    }
    
    /**
     * @dataProvider appProvider
     */
    public function testAddArea($appId)
    {
        $this->markTestSkipped();
        $result = $this->_model->addArea($appId, '电信一区', OaAppServerModel::IDC_TYPE_TELECOM);
        $this->assertTrue($result > 0);
        return $result;
    }
    
    public function testGetArea()
    {
        $this->markTestSkipped();
        $result = $this->_model->getArea(1800);
        $this->_logger->log($result, PEAR_LOG_INFO);
        $this->assertTrue(is_array($result));
    }
    
    /**
     * @dataProvider appProvider
     */
    public function testGetAppAreas($appId)
    {
        $this->markTestSkipped();
        $result = $this->_model->getAppAreas($appId);
        $this->_logger->log($result, PEAR_LOG_INFO);
        $this->assertTrue(is_array($result));
    }
    
    /**
     * @depends testAddArea
     */
    public function testModifyArea($areaId)
    {
        $this->markTestSkipped();
        $result = $this->_model->modifyArea($areaId, array(
            'name' => '电信二区'
        ));
        $this->assertTrue($result);
    }
    
    /**
     * @depends testAddArea
     */
    public function testDeleteArea($areaId)
    {
        $this->markTestSkipped();
        $result = $this->_model->deleteArea($areaId);
        $this->assertTrue($result);
    }
    
    /**
     * @dataProvider appProvider
     */
    public function testGetAppLoginServers($appId)
    {
        $this->markTestSkipped();
        $result = $this->_model->getAppLoginServers($appId);
        $this->_logger->log($result, PEAR_LOG_INFO);
        $this->assertTrue(is_array($result));
    }
    
    /**
     * @dataProvider appProvider
     */
    public function testGetAppPayServers($appId)
    {
        $this->markTestSkipped();
        $result = $this->_model->getAppPayServers($appId);
        $this->_logger->log($result, PEAR_LOG_INFO);
        $this->assertTrue(is_array($result));
    }
    
    /**
     * @dataProvider loginServerProvider
     */
    public function testGetLoginServerValidTo($loginServerId)
    {
        $this->markTestSkipped();
        $result = $this->_model->getLoginServerValidTo($loginServerId);
        $this->_logger->log($result, PEAR_LOG_INFO);
        $this->assertTrue(is_array($result));
    }
    
    /**
     * @dataProvider payServerProvider
     */
    public function testGetPayServerValidTo($payServerId)
    {
        $this->markTestSkipped();
        $result = $this->_model->getPayServerValidTo($payServerId);
        $this->_logger->log($result, PEAR_LOG_INFO);
        $this->assertTrue(is_array($result));
    }
    
    public function testGetConstantValueByName()
    {
        $this->markTestSkipped();
        $result = OaAppModel::getConstantValueByName('APP_TYPE_CLIENT');
        $this->_logger->log($result, PEAR_LOG_INFO);
        $this->assertTrue($result !== false);
        $result = OaAppModel::getConstantValueByName('app_type_client');
        $this->_logger->log($result, PEAR_LOG_INFO);
        $this->assertTrue($result !== false);
        $result = OaAppModel::getConstantValueByName('NOT_EXISTS', -1);
        $this->_logger->log($result, PEAR_LOG_INFO);
        $this->assertTrue($result === -1);
    }
    
    /**
     * @dataProvider appProvider
     */
    public function testDownloadSndaPubKey($appId)
    {
        $this->markTestSkipped();
        $result = $this->_model->downloadSndaPubKey($appId);
        $this->_logger->log($result, PEAR_LOG_INFO);
        $this->assertTrue($result !== false);
    }
    
    public function testGenerateGroupCode()
    {
        $this->markTestSkipped();
        $result = $this->_model->generateGroupCode(4);
        $this->_logger->log($result, PEAR_LOG_INFO);
        $this->assertTrue($result > 0);
    }
    
    public function testAddTestAccount()
    {
        $this->markTestSkipped();
        $result = $this->_model->addTestAccount('100000023', 'tytyty', 691497365);
        $this->_logger->log($result, PEAR_LOG_INFO);
        $this->assertTrue($result);
    }
    
    public function testCopyTestConfigToProduction()
    {
//        $this->markTestSkipped();
//        $this->_model->syncAppInfo(110058000, 'add', OaAppModel::APP_CONFIG_ENV_PRODUCTION);
        $result = $this->_model->copyTestConfigToProduction(110058000);
        $this->assertTrue($result);
    }
    
    public function testGetServerByIpAlias()
    {
        $this->markTestSkipped();
        $result = $this->_model->getServerByIpAlias(1, '192.168.1.9', 'a');
        $this->_logger->log($result, PEAR_LOG_INFO);
        $this->assertTrue(is_array($result));
    }
    
    public function testGetPayServerValidToById()
    {
        $this->markTestSkipped();
        $result = $this->_model->getPayServerValidToById(342);
        $this->_logger->log($result, PEAR_LOG_INFO);
        $this->assertTrue(is_array($result));
    }
    
    public function appProvider()
    {
        return array(
            array(11)
        );
    }
    
    public function loginServerProvider()
    {
        return array(
            array(10)
        );
    }
    
    public function payServerProvider()
    {
        return array(
            array(1)
        );
    }
}
